package com.example.wac.interceptor;

import com.example.wac.entity.User;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wuancheng
 * @description
 * @date 2023-03-20 17:55:00
 */

@Component
public class MyArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        // 这里是对参数做类型判断，也可以做注解判断
        // methodParameter.hasMethodAnnotation(xxx.class);
        return methodParameter.getParameterType().equals(User.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        // 解析参数
        HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest();
        return new User(1, "测试通过HandlerMethodArgumentResolver增加参数", "resolveArgument处理");
    }
}
